<?php
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
 
class ModCBirthday
{
    function getNames( $params )
	{
	
		$db =& JFactory::getDBO();
		$displayname = $params->get( 'displayname', 1 );
		$daysbefore = $params->get( 'daysbefore', 2 );
		$daysafter = $params->get( 'daysafter', 5 );

		switch($displayname) {
			case 1:
				$displayname_query = "u.name";
				break;
			case 0:
			default:
				$displayname_query = "u.username";
				break;
		}

		$query = "SELECT u.username, $displayname_query AS displayname, p.bdate, DATE_FORMAT(p.bdate, '%b %e') AS fbdate "
			. "\nFROM #__users AS u, #__congregation_profile AS p 
			WHERE  u.username=p.username AND
			(
			IF(DAYOFYEAR(bdate) >= DAYOFYEAR(NOW()),
			DAYOFYEAR(bdate) - DAYOFYEAR(NOW()),
			DAYOFYEAR(bdate) - DAYOFYEAR(NOW()) +
			DAYOFYEAR(CONCAT(YEAR(NOW()),'-12-31')))
			> (365 - $daysbefore )
			OR
			IF(DAYOFYEAR(bdate) >= DAYOFYEAR(NOW()),
			DAYOFYEAR(bdate) - DAYOFYEAR(NOW()),
			DAYOFYEAR(bdate) - DAYOFYEAR(NOW()) +
			DAYOFYEAR(CONCAT(YEAR(NOW()),'-12-31')))
			<= $daysafter
			)
			AND ddate='0000-00-00' AND status > 0
			";

		if(date("m", mktime()) == 12) {
			$query .= "
			ORDER BY DATE_FORMAT(bdate, '%m') DESC , DATE_FORMAT(bdate, '%m%d' )
			";
		} else {
			$query .= "
			ORDER BY DATE_FORMAT(bdate, '%m%d' )
			";
		}
	
	$db->setQuery( $query );

	return $db->loadObjectList();
	}
} 
